package handler

import (
	"fcs-api/src/app/business"
	"fcs-api/src/app/model"
	"net/http"

	"github.com/emicklei/go-restful"
)

func (api *APIHandler) handleCreateLayer(request *restful.Request, response *restful.Response) {
	layer, err := validateCreateLayer(request)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	layer, err = business.CreateLayer(layer)
	if err != nil {
		handleInternalError(response, model.InternalServerError(model.FunctionUpdateError, "创建层失败"))
		return
	}
	_ = response.WriteHeaderAndEntity(http.StatusOK, layer)
}

func (api *APIHandler) handleCreateLayerVersion(request *restful.Request, response *restful.Response) {
	layer, err := validateCreateLayerVersion(request)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	layer, err = business.CreateLayer(layer)
	if err != nil {
		handleInternalError(response, model.InternalServerError(model.FunctionUpdateError, "创建层版本失败"))
		return
	}
	_ = response.WriteHeaderAndEntity(http.StatusOK, layer)
}

func (api *APIHandler) handleGetLayerList(request *restful.Request, response *restful.Response) {
	query, err := validateGetLayerList(request)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	result, err := business.GetLayerList(query)
	if err != nil {
		handleInternalError(response, model.InternalServerError(model.FunctionUpdateError, "查询层列表失败"))
		return
	}
	_ = response.WriteHeaderAndEntity(http.StatusOK, result)
}

func (api *APIHandler) handleGetLayerVersionList(request *restful.Request, response *restful.Response) {
	rootId, err := validateGetLayerVersionList(request)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	result, err := business.GetLayerVersionList(rootId)
	if err != nil {
		handleInternalError(response, model.InternalServerError(model.FunctionUpdateError, "查询层列表失败"))
		return
	}
	_ = response.WriteHeaderAndEntity(http.StatusOK, result)
}

func (api *APIHandler) handleGetLayerRuntimeList(request *restful.Request, response *restful.Response) {
	runtime, err := validateGetLayerRuntimeList(request)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	user := currentUser(request)
	result, err := business.GetLayerRuntimeList(user.Id, runtime)
	if err != nil {
		handleInternalError(response, model.InternalServerError(model.FunctionUpdateError, "查询层列表失败"))
		return
	}
	_ = response.WriteHeaderAndEntity(http.StatusOK, result)
}

func (api *APIHandler) handleDeleteLayerVersion(request *restful.Request, response *restful.Response) {
	rootId, version, err := validateDeleteLayerVersion(request)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	err = business.DeleteLayerVersion(rootId, version)
	if err != nil {
		handleInternalError(response, model.InternalServerError(model.FunctionUpdateError, "删除层版本失败"))
		return
	}
	response.WriteHeader(http.StatusOK)
}

func (api *APIHandler) handleCheckLayerExist(request *restful.Request, response *restful.Response) {
	layerName := request.QueryParameter("layerName")
	user := currentUser(request)
	exist, err := model.CheckLayerExist(user.Id, layerName)
	if err != nil {
		_ = logger.Error("handleCheckGroupExist CheckGroupExist error %v", err)
	}
	_ = response.WriteHeaderAndEntity(http.StatusOK, map[string]interface{}{"exist": exist})
}
